/**
 * 字段权限页面
 */
var viewflag = true;
mini.parse();
var tree = mini.get("treegrid1");
var grid = mini.get("datagrid1");
grid.load(ajaxUrl + "/role/list.do");		//
grid.load();
var menuColumns = new ColumnsMenu(grid);
mini.get("sp").set({
	height : document.body.clientHeight - 40
});
var roleId;
//数据表选择
function onSelectionChanged(e) {
	console.log(e);
	var id = e.selected.roleId;
	roleId = id;
	// tree.load(ajaxUrl + "/roleMathData/findByRoleId.do?roleId=" + id);
	// tree.load("http://192.168.31.65:8080/bpm/roleMathData/findByRoleId.do?roleId=" + id);
	var url = ajaxUrl+"/roleMathData/findByRoleId.do?roleId=" + id

	$.ajax({
		type : 'POST',
		url : url,
		dataType : 'json',
		contentType: "application/json",
		success: function (data) {
			tree.setData(data);
			console.log(tree.getData());
			console.log(JSON.stringify(tree.getData()));

			if (viewflag) {
				tree
					.mask({
						html : "<div></div>",
						cls : "mini-panel",
						style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
					});
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			alert('获取失败！');
			// console.log(jqXHR);
		}
	})
}

//动态绘制按钮
function ondrawcell(e) {
	var tree = e.sender, record = e.record, column = e.column, field = e.field, id = record[tree
		.getIdField()], funs = record.functions;

	//按钮权限
	if (field == 'functions') {
		e.cellHtml = createCheckboxs(funs);
	}

	function createCheckboxs(funs) {
		if (!funs)
			return "";
		//如果有functions就执行
		var html = "";
		for (var i = 0, l = funs.length; i < l; i++) {
			var fn = funs[i];
			var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.action
				+ '\', this.checked)';
			var checked = fn.checked ? 'checked' : '';
			html += '<label class="function-item"><input onclick="' + clickFn
				+ '" ' + checked + ' type="checkbox" name="' + fn.action
				+ '" hideFocus/>' + fn.name + '</label>';
		}
		return html;
	}


}

//编辑
function update() {
	mini.get("but_update").disable();
	mini.get("but_save").enable();
	mini.get("but_abandon").enable();
	viewflag = false;
	tree.unmask();//右侧树可编辑
	grid.mask({//左侧表不可编辑
		html : "<div></div>",
		cls : "mini-panel",
		style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
	});

}

//放弃
function abandon() {
	mini.get("but_update").enable();
	mini.get("but_save").disable();
	mini.get("but_abandon").disable();
	viewflag = true;
	tree.mask({
		html : "<div></div>",
		cls : "mini-panel",
		style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
	});
	grid.unmask();

}

//保存
function save() {
	var nodes = tree.getCheckedNodes(true);
	console.log(nodes);

	var dataList = [];
	for(var  i = 0; i < nodes.length; i++){
		var nodeType = nodes[i].id.substring(0,2);
		//父级
		if(nodeType == "mo"){
			dataList.push({
				roleId: roleId,//角色
				userInfoId: 0,
				menuId: nodes[i].id.substring(nodes[i].id.indexOf("_")+1),//父级id
				roleDataList: []
			});
			//子级
		}else if(nodeType == "mu"){
			for(var j=0;j< dataList.length;j++) {
				if(dataList[j].menuId == nodes[i].pid.substring(nodes[i].id.indexOf("_")+1)) {
					// var id = nodes[i].id.substring(nodes[i].id.indexOf("_")+1);
					dataList[j].roleDataList.push(nodes[i].fileEN);
				}
			}

		}
	}
	console.log(dataList);
	//请求
	$.ajax({
		url     : ajaxUrl+"/roleMathData/insertRoleMathData.do",
		type    : "POST",
		async   : false,
		contentType : "application/json",
		data    : 	JSON.stringify(dataList),
		success : function (data){
			var uid = mini.alert("保存成功！");
		    setTimeout(function () {
		        var msgBox = mini.getByUid(uid);
		        mini.hideMessageBox(msgBox);
				abandon();
		    }, 2000)
		},
		error:function(data){
			mini.alert("保存失败！")
		}
	});
}

function resize() {
	if (viewflag) {
		tree.mask({
			html : "<div></div>",
			cls : "mini-panel",
			style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
		});
	}
}
function getFuns() {
	var data = tree.getData();
	var json = mini.encode(data);
}
function checkFunc(id, action, checked) {
	var record = tree.getRecord(id);		//得到指定行
	if (!record)
		return;
	var funs = record.functions;
	if (!funs)
		return;
	function getAction(action) {
		for (var i = 0, l = funs.length; i < l; i++) {
			var o = funs[i];
			if (o.action == action)
				return o;
		}
	}
	var obj = getAction(action);
	if (!obj)
		return;
	obj.checked = checked;
}

function onFilterChanged(e) {
	var rcValue = mini.get("rcFilter").getValue().toLowerCase();
	var rnValue = mini.get("rnFilter").getValue().toLowerCase();
	var rdValue = mini.get("rdFilter").getValue().toLowerCase();
	grid.filter(function(row) {
		var r1 = true;
		if (rcValue) {
			r1 = String(row.roleCode).toLowerCase().indexOf(rcValue) != -1;
		}
		var r2 = true;
		if (rnValue) {
			r2 = String(row.roleCode).toLowerCase().indexOf(rnValue) != -1;
		}
		var r3 = true;
		if (rdValue) {
			r3 = String(row.roleCode).toLowerCase().indexOf(rdValue) != -1;
		}
		return r1 && r2 && r3;
	});
}
//数组去重
function uniq(array){
	var temp = [];
	for(var i = 0; i < array.length; i++) {
		//如果当前数组的第i项在当前数组中第一次出现的位置是i，才存入数组；否则代表是重复的
		if(array.indexOf(array[i]) == i){
			temp.push(array[i])
		}
	}
	return temp;
}